// https://www.luogu.com.cn/problem/P4163
// 排列

int n, m;
int a[maxn];
int f[max_size][1005]; // 状态 s，% m 为 j 的方案数
// f[s | (1 << i)][(j * 10 + a[i]) % m] += f[s][j]

bool visit[maxn];
int dp() {
    memset(f, 0, sizeof(f));
    f[0][0] = 1;
    for (int s = 0, ed = 1 << n; s < ed; ++s) {
        memset(visit, 0, sizeof(visit));
        for (int i = 0; i < n; ++i) {
            if (s & (1 << i) || visit[a[i]]) continue;
            visit[a[i]] = 1;
            for (int j = 0; j < m; ++j)
                f[s | (1 << i)][(j * 10 + a[i]) % m] += f[s][j];
        }
    }
    return f[(1 << n) - 1][0];
}
